﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace _040
{
    internal class Program
    {
        static void Main(string[] args)
        {

            BinaryWriter bw;
            BinaryReader br;
            int i = 25;
            double d = 3.14157;
            bool b = true;
            string s = "C#中文网";
            //创建文件
            try
            {
                bw = new BinaryWriter(new FileStream("mydata", FileMode.Create));
            }
            catch(IOException e)
            {
                Console.WriteLine(e.Message+"\n文件创建失败！");
                return;
            }
            //写入文件
            try
            {
                bw.Write(i);
                bw.Write(d);
                bw.Write(b);
                bw.Write(s);
            }
            catch(IOException e)
            {
                Console.WriteLine(e.Message+"\n文件写入失败");
            }
            bw.Close();
            //读取文件
            try
            {
                br = new BinaryReader(new FileStream("mydata", FileMode.Open));
            }
            catch(IOException e)
            {
                Console.WriteLine(e.Message+"\n文件打开失败！");
                return;
            }
            try
            {
                i = br.ReadInt32();
                Console.WriteLine("Integer data:{0}",i);
                d = br.ReadDouble();
                Console.WriteLine("Double data:{0}",d);
                b = br.ReadBoolean();
                Console.WriteLine("Boolean data:{0}",b);
                s = br.ReadString();
                Console.WriteLine("String data:{0}",s);
            }
            catch(IOException e)
            {
                Console.WriteLine(e.Message+"\n文件读取失败！");
            }
            br.Close();
            Console.ReadKey();
        }
    }
}
